package com.framework.view.indicator;

import android.view.View;

import com.framework.view.indicator.configs.LayoutConfig;
import com.framework.view.indicator.indicators.configs.AbstractIndicatorConfig;
import com.framework.view.indicator.listener.OnItemClickListener;

import java.util.List;

/**
 * 自定义标题布局的适配器
 *
 * @author liangzx
 * @version 1.0
 * @time 2020-04-21 18:20
 **/
public abstract class LayoutIndicatorAdapter<K extends AbstractIndicatorConfig, T extends Object> extends BaseIndicatorAdapter<K> {

    /**
     * 数据列表
     */
    public List<T> mDatas;
    /**
     * 布局资源ID
     */
    private int mItemLayoutId;
    /**
     * 子控件点击事件
     */
    private OnItemClickListener mOnItemClickListener;

    public LayoutIndicatorAdapter(int layoutId) {
        this.mItemLayoutId = layoutId;
    }

    public LayoutIndicatorAdapter(int layoutId, List<T> datas) {
        this(layoutId);
        mDatas = datas;
    }

    /**
     * 绑定标题配置
     *
     * @return
     */
    protected abstract LayoutConfig bindTitleConfig();

    /**
     * 获取标题配置
     *
     * @return
     */
    final LayoutConfig getTitleConfig() {
        return bindTitleConfig();
    }

    /**
     * 绑定每个位置下的布局视图
     *
     * @param view     item布局
     * @param position
     * @return
     */
    protected abstract View bindItemTitleView(View view, int position, T data);

    /**
     * 获取对应位置数据
     *
     * @param position
     * @return T
     */
    public T getItem(int position) {
        if (null == mDatas || mDatas.size() <= position) {
            return null;
        }
        return mDatas.get(position);
    }

    @Override
    public int getCount() {
        if (null == mDatas) {
            return 0;
        }
        return mDatas.size();
    }

    /**
     * 返回布局资源id
     *
     * @return
     */
    public int getItemLayoutId() {
        return mItemLayoutId;
    }

    /**
     * 更新数据
     *
     * @param datas
     */
    public void updateDatas(List<T> datas) {
        this.mDatas = datas;
        notifyDataSetChanged();
    }

    /**
     * 设置子控件点击事件
     *
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    /**
     * 返回子控件点击事件
     * @return
     */
    OnItemClickListener getOnItemClickListener() {
        return mOnItemClickListener;
    }


}
